<template>
  <div>
    <el-button
      type="primary"
      icon="el-icon-plus"
      style="margin-bottom: 20px"
      @click="add"
      >添加</el-button
    >

    <!-- 表格 -->
    <el-table :data="trademarkInfo.records" stripe border style="width: 100%">
      <el-table-column align="center" type="index" label="序号" width="80">
      </el-table-column>
      <el-table-column prop="tmName" label="品牌名称" width="width">
      </el-table-column>
      <el-table-column prop="prop" label="品牌LOGO" width="width">
        <template slot-scope="{ row, $index }">
          <img :src="row.logoUrl" alt="" width="80" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="width">
        <template slot-scope="{ row }">
          <el-button
            style="margin-right: 10px"
            @click="edit(row)"
            type="warning"
            icon="el-icon-edit"
            size="mini"
            >修改</el-button
          >

          <el-popconfirm title="确定删除吗？" @onConfirm="delete1(row.id)">
            <el-button
              slot="reference"
              type="danger"
              icon="el-icon-delete"
              size="mini"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      style="text-align: center"
      @size-change="handleSizeChange"
      @current-change="getTrademarkList"
      :current-page="page"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      prev-text="上一页"
      next-text="下一页"
      background
      layout="prev, pager, next, jumper,->,total, sizes"
      :total="trademarkInfo.total"
    >
    </el-pagination>

    <!-- Dialog -->
    <el-dialog
      :title="form.id ? '修改品牌' : '添加品牌'"
      :visible.sync="dialogFormVisible"
    >
      <el-form ref="form" :model="form" label-width="100px" :rules="rules">
        <el-form-item label="品牌名称" prop="tmName">
          <el-input v-model="form.tmName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="品牌LOGO" prop="logoUrl">
          <el-upload
            drag
            ref="upload"
            class="avatar-uploader"
            action="/api/admin/product/upload"
            :show-file-list="true"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img
              v-if="form.logoUrl"
              :src="form.logoUrl"
              class="avatar"
              style="width: 100%; height: 100%"
            />
            <i
              style="font-size: 20px"
              v-else
              class="el-icon-plus avatar-uploader-icon"
              >此处支持拖拽</i
            >
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdateTrademark"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Trademark",
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请上传图片"));
      }  {
        callback();
      }
    };
    return {
      limit: 3,
      page: 1,
      trademarkInfo: [],
      dialogFormVisible: false,
      form: {
        tmName: "",
        logoUrl: "",
      },
      rules: {
        tmName: [
          { required: true, message: "请输入品牌名称", trigger: "blur" },
        ],
        logoUrl: [
          // { required: true, message: "请输入品牌LOGO" },
          { validator: validatePass2 },
        ],
      },
      imageUrl: "",
    };
  },
  mounted() {
    this.getTrademarkList();
  },

  methods: {
    async delete1(id) {
      try {
        let res = await this.$http.trademark.reqDeleteTrademark(id);
        if (res.code == 200) {
          this.$message({ type: "success", message: "删除成功" });
          this.getTrademarkList(
            this.trademarkInfo.records.length <= 1 ? this.page - 1 : this.page
          );
        }
      } catch (error) {}
    },
    //每页展示多少条数据
    handleSizeChange(size) {
      this.limit = size;
      this.getTrademarkList();
    },
    add() {
      this.dialogFormVisible = true;
      this.form = {
        tmName: "",
        logoUrl: "",
      };
      this.$nextTick(() => {
        this.$refs.upload.clearFiles();
        this.$refs.form.resetFields();
      });
    },
    edit(row) {
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs.upload.clearFiles();
        this.$refs.form.resetFields();
        this.form = { ...row };
      });
    },
    //添加或更新品牌
    async addOrUpdateTrademark() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          try {
            await this.$http.trademark.reqAddOrUpdateTrademark(this.form);
            this.$message({
              message: this.form.id ? "恭喜您，修改成功" : "恭喜您,添加成功",
              type: "success",
            });
            this.getTrademarkList(this.form.id ? this.page : 1);
            this.dialogFormVisible = false;
          } catch (error) {
            this.$message({
              message: this.form.id ? "修改失败" : "添加失败",
              type: "success",
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleAvatarSuccess(res, file) {
      this.form.logoUrl = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    //获取品牌列表
    async getTrademarkList(p = 1) {
      this.page = p;
      let { page, limit } = this;
      let res = await this.$http.trademark.reqTrademarkList(page, limit);
      if (res.code == 200) {
        this.trademarkInfo = res.data;
      }
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

</style>

<style lang="scss" scoped>
::v-deep .number{
  color:red !important;
}
</style>